package com.app.micaihu.custom.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.bean.infor.SignLasting;
import com.app.utils.util.DateUtils;
import com.app.utils.util.ViewSizeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Guo on 2016/9/20.
 * <p/>
 * 连续签到的自定义view
 */
public class SignLastingView extends RelativeLayout {

    private Context mContext;
    private int widthPixels;//屏幕的宽
    private int dp3;
    private Paint unSignPaint;
    private Paint signedPaint;
    private Paint textPaint;
    private float point1;
    private float point2;
    private float point3;
    private float point4;
    private float point5;
    private float point6;
    private float point7;
    private float viewMiddle;
    private float textY;

    private List<Float> pointList;//点的集合
    private List<String> currentDateList = new ArrayList<>();//当前日期至后续7天的日期str集合


    private List<SignLasting> signList;//连续签到的数据
    private float radius;
    private float itemWidth;
    private Paint unSignedPaintCircle;
    private Paint signedPaintCircle;
    private Paint textPaint2;
    private float priceY;
    private float localX;
    private int margin;


    public SignLastingView(Context context) {
        this(context, null);
    }

    public SignLastingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SignLastingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;

        pointList = new ArrayList<>();

        setWillNotDraw(false);
        widthPixels = ViewSizeUtils.getWidthPixels();

        dp3 =  ViewSizeUtils.dp2px(mContext, 3);

        //初始化画笔
        //已签到的线条
        signedPaint = new Paint();
        signedPaint.setAntiAlias(true);
        signedPaint.setDither(true);
        signedPaint.setColor(mContext.getResources().getColor(R.color.usertask_signlasting));
        signedPaint.setStrokeWidth(dp3);
        signedPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        //未签到线条的灰色
        unSignPaint = new Paint();
        unSignPaint.setAntiAlias(true);
        unSignPaint.setDither(true);
        unSignPaint.setColor(mContext.getResources().getColor(R.color.usertask_unsignlasting));
        unSignPaint.setStrokeWidth(dp3);
        unSignPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        //已签到的圆的颜色
        signedPaintCircle = new Paint();
        signedPaintCircle.setAntiAlias(true);
        signedPaintCircle.setDither(true);
        signedPaintCircle.setColor(mContext.getResources().getColor(R.color.usertask_signlasting));
        signedPaintCircle.setStyle(Paint.Style.FILL);

        //未签到的圆的颜色
        unSignedPaintCircle = new Paint();
        unSignedPaintCircle.setAntiAlias(true);
        unSignedPaintCircle.setDither(true);
        unSignedPaintCircle.setColor(mContext.getResources().getColor(R.color.usertask_unsignlasting));
        unSignedPaintCircle.setStyle(Paint.Style.FILL);


        //文字的
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setDither(true);
        textPaint.setColor(mContext.getResources().getColor(R.color.usertask_signlasting));
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setStyle(Paint.Style.STROKE);
        textPaint.setTextSize(mContext.getResources().getDimension(R.dimen.common_font_size11));


        textPaint2 = new Paint();
        textPaint2.setAntiAlias(true);
        textPaint2.setDither(true);
        textPaint2.setColor(mContext.getResources().getColor(R.color.usertask_unsignlasting));
        textPaint2.setTextAlign(Paint.Align.CENTER);
        textPaint2.setStyle(Paint.Style.STROKE);
        textPaint2.setTextSize(mContext.getResources().getDimension(R.dimen.common_font_size11));

        //获取当前的事件x.xx的形式
//        String currentDayStr = DateUtils.getCurrentTime("M.d");
//        LogUtils.e("gzq","currentDayStr-----------------------------"+currentDayStr);
        for (int i = 0;i<7;i++){
            String date = DateUtils.afterNDay("M.d", i);
            if(currentDateList == null){
                currentDateList = new ArrayList<>();
            }
//            LogUtils.e("gzq","currentDayStr-----------------------------"+date);
            currentDateList.add(date);

        }

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        calculateLoaction();
        //LogUtils.e("gzq", "前");
        drawView(canvas);
        //LogUtils.e("gzq", "后");

    }


    private float rootLeft;
    private float rootTop;
    private float rootRight;
    private float rootBottom;


    private void calculateLoaction() {



        rootLeft = getLeft();//控件左边
        rootTop = getTop();
        rootRight = getRight();//控件右边
        rootBottom = getBottom();


        margin = ViewSizeUtils.dp2px(AppApplication.getInstance(), 18);
        //确定 圆的半径

        float width = rootRight - rootLeft;   //拿到控件的长度
        //LogUtils.e("gzq","控件的长="+width);
        radius = width/86;//分成43分的的球，半径是控件的86等分;
        radius = ViewSizeUtils.dp2px(AppApplication.getInstance(),4);
        //LogUtils.e("gzq","radius="+radius);
        //线的长度
        float lineWidth = width - (radius * 2 * 7) - (margin * 2);//纯线的长度，不包括点,还要减去两边的间距
        //float lineWidth2 = width/43 * 6 * 6;

        //LogUtils.e("gzq","线的长="+lineWidth +"---lineWidth2="+lineWidth2);

        itemWidth = lineWidth / 6; //单个条目的长度
        //LogUtils.e("gzq","单线的长="+itemWidth);
        //Y轴的中心点
        viewMiddle = (rootBottom - rootTop) / 2;

        textY = viewMiddle + radius * 5;
        priceY = viewMiddle - radius * 6;


        //LogUtils.e("gzq","中心点="+viewMiddle+"---textY="+textY);
        point1 = rootLeft + margin + radius;
        point2 = point1 + itemWidth + radius *2;
        point3 = point2 + itemWidth + radius *2;
        point4 = point3 + itemWidth + radius *2;
        point5 = point4 + itemWidth + radius *2;
        point6 = point5 + itemWidth + radius *2;
        point7 = point6 + itemWidth + radius *2;

        if(pointList != null){
            pointList.clear();
            pointList.add(point1);
            pointList.add(point2);
            pointList.add(point3);
            pointList.add(point4);
            pointList.add(point5);
            pointList.add(point6);
            pointList.add(point7);

        }

        //LogUtils.e("gzq","point1="+point1+"-point2="+point2+"-point3="+point3+"-point4+"+point4+"-point5+"+point5+"-point6+"+point6+"-point7+"+point7);

    }

    private void drawView(Canvas canvas) {
//        LogUtils.e("gzq","signList=="+(signList == null));
//        LogUtils.e("gzq","pointList=="+(pointList == null));
//        LogUtils.e("gzq","signList=="+signList.size());
//        LogUtils.e("gzq","pointList=="+pointList.size());

        if (this.signList != null && this.signList.size() == 7 && pointList != null && pointList.size() == 7) {
            //LogUtils.e("gzq","draw方法中--有数据"+"-时间--"+System.currentTimeMillis());
            localX = -1;
            for (int i = 0; i < this.signList.size(); i++) {
                //画圆
//                LogUtils.e("gzq","设置集合后的数据--签到与否="+signList.get(i).getIsSign()+"--日期="+signList.get(i).getDate());
                if (signList.get(i).getIsSign()) {
                    canvas.drawCircle(pointList.get(i), viewMiddle, radius, signedPaintCircle);
                    canvas.drawText(signList.get(i).getDate(), pointList.get(i), textY, textPaint);
                } else {
                    canvas.drawCircle(pointList.get(i), viewMiddle, radius, unSignedPaintCircle);
                    canvas.drawText(signList.get(i).getDate(), pointList.get(i), textY, textPaint2);
                }
                //线
                if (i < 6) {
                    if (signList.get(i).getIsSign() && signList.get(i+1).getIsSign()) {
                        canvas.drawLine(pointList.get(i) + radius, viewMiddle, pointList.get(i) + itemWidth + radius*2, viewMiddle, signedPaint);
                    } else {
                        canvas.drawLine(pointList.get(i) + radius, viewMiddle, pointList.get(i) + itemWidth+ radius*2, viewMiddle, unSignPaint);
                    }
                }

                if(i+1 < signList.size() && signList.get(i).getIsSign() != signList.get(i+1).getIsSign()){
                    localX = pointList.get(i);
                }
            }
            BitmapFactory.Options op=new BitmapFactory.Options();
            op.inJustDecodeBounds=true;
            //获取图片的option 属性.
            Bitmap bitmapLocal = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.task_sign_today, op);
            int outWidth1 = op.outWidth;
//            LogUtils.e("gzq","outWidth=="+outWidth);
            //画当前 的游标节点
//            LogUtils.e("gzq","localX=="+localX);
            if(pointList.contains(localX)){
//                LogUtils.e("gzq","huizhi==outWidth="+outWidth+"--localX="+localX+"--");
                canvas.drawBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.task_sign_today), localX -outWidth1/2 - radius/4 +2, viewMiddle - radius - radius/2, null);
//                canvas.drawBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.task_sign_today), localX -outWidth1/2 , viewMiddle - radius - radius/2, null);
            }
            //画奖励图标
//            Bitmap bitmapLoca2 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.task_sign_reward, op);
            int outWidth2 = op.outWidth;

            canvas.drawBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.task_sign_reward),point7 - outWidth2/2,priceY,null);
//            canvas.drawBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.task_sign_reward),point7 - radius * 2,priceY,null);



        } else {
//            LogUtils.e("gzq","draw方法中--没数据"+"时间--"+System.currentTimeMillis());
            for (int i = 0; i < 7; i++) {
                //画圆
                canvas.drawCircle(pointList.get(i), viewMiddle, radius, unSignPaint);
                //画日期
                if(currentDateList != null && currentDateList.size()>0){
                    canvas.drawText(currentDateList.get(i), pointList.get(i), textY, textPaint2);
                }
                if (i <6) {
                    canvas.drawLine(pointList.get(i) + radius, viewMiddle, pointList.get(i) + itemWidth+ radius*2, viewMiddle, unSignPaint);
                }

            }
        }

    }


    public void setSignList(List<SignLasting> list) {

       /* for (int i = 0;i<7;i++){
            list.get(i).setIsSign("1");

        }*/


        this.signList = list;
    /*    LogUtils.e("gzq","外部设置数据集合");
        for (int i = 0;i<7;i++){

            String date = this.signList.get(i).getDate();
            boolean isSign = this.signList.get(i).getIsSign();
            LogUtils.e("gzq","date="+date+"--isSign="+isSign);
        }*/

        invalidate();
    }

}
